package com.xyz.keshe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.xyz.keshe.R;
import com.xyz.keshe.model.Admin;

import java.util.List;

public class AdminManageAdapter extends BaseAdapter {

    private Context context;
    private List<Admin> admins;
    private OnAdminActionListener listener;

    public interface OnAdminActionListener {
        void onEdit(Admin admin);
        void onDelete(Admin admin);
    }

    public AdminManageAdapter(Context context, List<Admin> admins, OnAdminActionListener listener) {
        this.context = context;
        this.admins = admins;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return admins.size();
    }

    @Override
    public Object getItem(int position) {
        return admins.get(position);
    }

    @Override
    public long getItemId(int position) {
        return admins.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_admin_manage, parent, false);
        }

        Admin admin = admins.get(position);

        TextView tvUsername = convertView.findViewById(R.id.tv_admin_username);
        TextView tvType = convertView.findViewById(R.id.tv_admin_type);
        Button btnEdit = convertView.findViewById(R.id.btn_edit_admin);
        Button btnDelete = convertView.findViewById(R.id.btn_delete_admin);

        tvUsername.setText(admin.getUsername());
        tvType.setText(admin.getIsSuper() ? "超级管理员" : "普通管理员");

        btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEdit(admin);
            }
        });

        btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDelete(admin);
            }
        });

        // admin用户不显示编辑和删除按钮
        if ("admin".equals(admin.getUsername())) {
            btnEdit.setVisibility(View.GONE);
            btnDelete.setVisibility(View.GONE);
        } else {
            btnEdit.setVisibility(View.VISIBLE);
            btnDelete.setVisibility(View.VISIBLE);
        }

        return convertView;
    }
} 